package com.ts.simplemusic;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MusicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MusicFragment extends Fragment {

    private static final String TAG = "MusicFragment";

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View mView;

    private String[] data = {"路过人间", "旅行", "灰色轨迹", "水墨丹青", "这世界那么多人", "百年孤独", "其实都没有", "明天过后", "不谓侠", "平凡之路", "病变"};

    private List<Music> musicList = new ArrayList<>();
    private MusicAdapter mMusicAdapter;
    private RecyclerView mMusicRecView;
    private MainActivity mainActivity;

    public MusicFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MusicFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MusicFragment newInstance(String param1, String param2) {
        MusicFragment fragment = new MusicFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_music, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mView = view;
        initMusic();
        initView();
        initListener();
    }

    private void initListener() {
        mMusicAdapter.setClickLister(new MusicAdapter.onClickLister() {
            @Override
            public void replace() {
                mainActivity.replacePlayFragment();
            }
        });
    }

    private void initView() {
        mainActivity = (MainActivity) requireActivity();
        mMusicAdapter = new MusicAdapter(musicList);
        mMusicRecView = mView.findViewById(R.id.rec_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(requireContext());
        mMusicRecView.setLayoutManager(linearLayoutManager);
        mMusicRecView.setAdapter(mMusicAdapter);
    }

    private void initMusic() {
        for (String datum : data) {
            Music music = new Music();
            music.setTitle(datum);
            musicList.add(music);
        }
        // 更新MusicHelper的音乐播放集合
        MusicHelper.getInstance().setMusicList(musicList);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
        // 更新选中态，与用户在播放界面切歌保持一致
        if (mMusicAdapter.getCurrentPosition() != MusicHelper.getInstance().getCurrentPosition()) {
            mMusicAdapter.notifyItemChanged(mMusicAdapter.getCurrentPosition(), 0);
            mMusicAdapter.notifyItemChanged(MusicHelper.getInstance().getCurrentPosition(), 0);
        }
    }
}